﻿namespace Week3_1_Repository
{
    public class ORepository<T> where T : class
    {
        ODBContext db;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        public ORepository(ODBContext db)
        {
            this.db = db;
        }

        /// <summary>
        /// 添加方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns>返回受影响的行数</returns>
        public int Add(T entity)
        {
            db.Add(entity);
            return db.SaveChanges();
        }
        /// <summary>
        /// 显示方法
        /// </summary>
        /// <returns>返回受影响的实体</returns>
        public IQueryable<T> GetAll()
        {
            return db.Set<T>().AsQueryable();
        }
        /// <summary>
        /// 反填方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns>返回受影响的实体</returns>
        public T GetByid(int id)
        {
            return db.Set<T>().Find(id);
        }
        /// <summary>
        /// 修改方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns>返回受影响的行数</returns>
        public int Update(T entity)
        {
            db.Update(entity);
            return db.SaveChanges();
        }
        /// <summary>
        /// 批量修改方法
        /// </summary>
        /// <param name="values"></param>
        /// <returns>返回受影响的行数</returns>
        public int PiUpd(List<T> values)
        {
            db.UpdateRange(values);
            return db.SaveChanges();
        }















    }
}
